/**
 * 
 */
package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.vectrics.common.util.DateUtil;
import org.vectrics.employee.Employee;

import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.EmployeeLeavesReportDTO;
import com.hexacta.hrs.services.employee.EmployeeService;

/**
 * @author mfunes
 * 
 */
public class EmployeeLeavesReport extends AbstractReport {

    private EmployeeService employeeService;

    private Date dateFrom;

    private Date dateTo;

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.AbstractReport#getReportData(java.util.Map)
     */
    @Override
    @SuppressWarnings("unchecked")
    protected Collection getReportData(Map<String, Object> filters) {
        Collection<EmployeeLeavesReportDTO> collection = new ArrayList<EmployeeLeavesReportDTO>();

        String pivotAux;
        Collection<Employee> employeeList = employeeService.getNoLongerEmployees();

        if (filters == null) {
            filters = new HashMap<String, Object>();
        }

        dateTo = (Date) filters.get("lastWorkingDateTo");
        if (dateTo == null) {
            insertDefaultDateTo();
        }

        dateFrom = (Date) filters.get("lastWorkingDateFrom");
        if (dateFrom == null) {
            insertDefaultDateFrom();
        }

        for (Employee employee : employeeList) {

            EmployeeLeavesReportDTO reportDTO = new EmployeeLeavesReportDTO();

            if (employee.getDateEnd() != null) {
                pivotAux = DateUtil.getFechaFormateada(employee.getDateEnd());
                reportDTO.setLastWorkingDay(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
            }

            Date lastWorking = DateUtil.getFecha(reportDTO.getLastWorkingDay());

            if (lastWorking != null && DateUtil.isBetween(lastWorking, dateFrom, dateTo)) {

                if (employee.getPerson() != null) {
                    reportDTO.setFirstName(employee.getPerson().getFirstName());
                    reportDTO.setLastName(employee.getPerson().getLastName());
                    reportDTO.setCuilOrCuit(employee.getPerson().getCuilOrCuit());
                }

                if (employee.getFunction() != null) {
                    reportDTO.setCareerRole(employee.getFunction().getDescription());
                }

                if (employee.getCareerRolePosition() != null) {
                    reportDTO.setCareerRolePosition(employee.getCareerRolePosition().toString());
                }

                if (employee.getDateStart() != null) {
                    pivotAux = DateUtil.getFechaFormateada(employee.getDateStart());
                    reportDTO.setFirstWorkingDay(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
                }

                if (employee.getFirstEmployeeDate() != null) {
                    pivotAux = DateUtil.getFechaFormateada(employee.getFirstEmployeeDate());
                    reportDTO.setFirstEmployeeDay(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
                }

                if (employee.getLocation() != null) {
                    reportDTO.setOfficeLocation(employee.getLocation().getAbbreviation());
                }

                collection.add(reportDTO);
            }
        }
        // }
        if (collection.isEmpty())
            throw new RuntimeException("There is no data for this Report");
        Collections.sort((List) collection);
        return collection;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.AbstractReport#getReportParameters()
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Map getReportParameters() {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createPDFReport(java.lang.String,
     * java.lang.String, java.util.Map)
     */
    @Override
    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /** {@inheritDoc} */
    @Override
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }

    public Date getDateFrom() {
        return dateFrom;
    }

    public void setDateFrom(final Date dateFrom) {
        this.dateFrom = dateFrom;
    }

    public Date getDateTo() {
        return dateTo;
    }

    public void setDateTo(final Date dateTo) {
        this.dateTo = dateTo;
    }

    @SuppressWarnings("deprecation")
    private void insertDefaultDateFrom() {
        Calendar actual = Calendar.getInstance();
        actual.set(Calendar.YEAR, getDateTo().getYear() - 1900);
        setDateFrom(actual.getTime());
        // this.getDateFrom().setMonth(11);
        // this.getDateFrom().setDate(31);
    }

    private void insertDefaultDateTo() {
        Calendar actualDate = Calendar.getInstance();
        setDateTo(actualDate.getTime());
    }
}
